#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <cstring>


//int main(){
//    int fd;
//    char *data=NULL;
//    int data_len;
//    socklen_t addt_len;
//    struct sockaddr_in server_addr,from_addr;
//    fd=socket(PF_INET,SOCK_DGRAM,0);
//    if(fd==-1)
//        perror("socket");
//    memset(&server_addr,0,sizeof(server_addr));
//    memset(&from_addr,0, sizeof(from_addr));
//    server_addr.sin_family=AF_INET;
//    server_addr.sin_addr.s_addr=inet_addr("192.168.141.128");
//    server_addr.sin_port=htons(9090);
//    from_addr.sin_addr.s_addr=inet_addr("192.168.1.231");
//    from_addr.sin_port=htons(9090);
////    if(bind(fd,(struct sockaddr*)&server_addr,sizeof(server_addr))==-1)
////        perror("bind:");
//    data_len= sizeof(from_addr);
//    data_len=recvfrom(fd, data, 1024, 0,
//                      (struct sockaddr*)&from_addr, reinterpret_cast<socklen_t *>(&data_len));
//    if(data_len==-1){
//        perror("recvfrom:");
//    }else if(data_len>0)
//        std::cout<<data<<std::endl;
//    else
//        std::cout<<"fail in recvfrom"<<std::endl;
//    close(fd);
//    return 0;
//}

#define BUFFSIZE 1024

int main(int argc, char *argv[])
{
    int client_sockfd = 0;
    int len = 0;
    struct sockaddr_in server_addr;
    int sin_size = 0;
    char buf[BUFFSIZE] = {0};
    bzero(&server_addr, sizeof(struct sockaddr_in));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr("192.168.141.128");
    server_addr.sin_port = htons(8000);

    if((client_sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket error!\n");
        return -1;
    }

    sin_size = sizeof(struct sockaddr_in);

    while(1)
    {
        scanf("%s",buf);
        if(!strcmp(buf,"quit"))
        {
            break;
        }

        if((len = sendto(client_sockfd, buf, strlen(buf), 0 ,(struct sockaddr*)&server_addr, sizeof(struct sockaddr))) < 0)
        {
            perror("sendto fail!\n");
            return -1;
        }
    }
    close(client_sockfd);

    return 0;
}